package com.shimao.celnet.payment.controller.apinotcheck;

import com.baiwang.bop.client.BopException;
import com.baiwang.bop.client.IBopClient;
import com.baiwang.bop.request.impl.invoice.common.InvoiceMainInfo;
import com.baiwang.bop.request.impl.invoice.impl.InvoiceQueryRequest;
import com.baiwang.bop.respose.entity.InvoiceQueryResponse;
import com.shimao.celnet.payment.model.PInvoiceBlue;
import com.shimao.celnet.payment.model.PInvoiceRed;
import com.shimao.celnet.payment.service.PInvoiceBlueService;
import com.shimao.celnet.payment.service.PInvoiceRedService;
import com.shimao.celnet.payment.service.apiservice.CallOut_invOpenOutService;
import com.shimao.celnet.payment.util.MyDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 发票查询接口 支付平台到--》百望平台
 * christ
 *2018/01/18
 */
@Service
public class CallOut_invoiceQueryOutController {

    @Autowired
    private CallOut_invOpenOutService invOpenOutService;

    @Autowired
    private PInvoiceRedService pInvoiceRedService;

    @Autowired
    private PInvoiceBlueService pInvoiceBlueService;
    /**
     * 通过发票代码和发票号码进行查询蓝票
     * @param pInvoiceBlueRet
     * @return
     */
    public InvoiceQueryResponse invoiceBlueQueryOut(PInvoiceBlue pInvoiceBlueRet){
        try
        {
            InvoiceQueryRequest request = new InvoiceQueryRequest();
            request.setSellerTaxNo(pInvoiceBlueRet.getSellerTaxNo());//销方税号，查询条件
            request.setInvoiceQueryType("2");//查询类型 1发票流水号查询  2发票号码和发票代码查询 3纳税人识别号【销方】  4开票终端标识 5开票日期起止  6.购方信息
            request.setInvoiceStartDate(pInvoiceBlueRet.getInvoiceStartTime());//开票日期起，格式：yyyyMMddHHmmss
            request.setInvoiceEndDate(pInvoiceBlueRet.getInvoiceEndTime());//开票日期止，格式：yyyyMMddHHmmss
            request.setInvoiceCode(pInvoiceBlueRet.getInvoiceCode());//发票代码
            request.setInvoiceNo(pInvoiceBlueRet.getInvoiceNo());//发票号码
            String token = invOpenOutService.getToken();//获取百望token
            IBopClient client = invOpenOutService.getClient();
            InvoiceQueryResponse response = client.execute(request, token, InvoiceQueryResponse.class);//根据请求百望需要的参数，token查询并响应回来信息
            for (InvoiceMainInfo invoiceMainInfo : response.getInvoiceList()){
                pInvoiceBlueRet.setInvoiceTotalPrice(MyDateUtil.float2Int1(invoiceMainInfo.getInvoiceTotalPrice()));//合计金额
                pInvoiceBlueRet.setInvoiceTotalTax(MyDateUtil.float2Int1(invoiceMainInfo.getInvoiceTotalTax()));//合计税额
                pInvoiceBlueRet.setInvoiceTotalPriceTax(MyDateUtil.float2Int1(invoiceMainInfo.getInvoiceTotalPriceTax()));//价税合计
            }
            pInvoiceBlueService.update(pInvoiceBlueRet);//保存数据
            return response;
        }
        catch (BopException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 红票查询接口
     * @param pInvoiceRedRet
     * @return
     */
    public InvoiceQueryResponse invoiceRedQueryOut( PInvoiceRed pInvoiceRedRet){
        try
        {
            InvoiceQueryRequest request = new InvoiceQueryRequest();
            request.setSellerTaxNo(pInvoiceRedRet.getSellerTaxNo());//销方税号，查询条件
            request.setInvoiceQueryType("2");//查询类型 1发票流水号查询  2发票号码和发票代码查询 3纳税人识别号【销方】  4开票终端标识 5开票日期起止  6.购方信息
            request.setInvoiceStartDate(pInvoiceRedRet.getInvoiceStartTime());//开票日期起，格式：yyyyMMddHHmmss
            request.setInvoiceEndDate(pInvoiceRedRet.getInvoiceEndTime());//开票日期止，格式：yyyyMMddHHmmss
            request.setInvoiceCode(pInvoiceRedRet.getInvoiceCode());//发票代码
            request.setInvoiceNo(pInvoiceRedRet.getInvoiceNo());//发票号码
            String token = invOpenOutService.getToken();//获取百望token
            IBopClient client = invOpenOutService.getClient();
            InvoiceQueryResponse response = client.execute(request, token, InvoiceQueryResponse.class);
            for (InvoiceMainInfo invoiceMainInfo : response.getInvoiceList()){
                pInvoiceRedRet.setInvoiceTotalPrice(MyDateUtil.float2Int1(invoiceMainInfo.getInvoiceTotalPrice()));//合计金额
                pInvoiceRedRet.setInvoiceTotalTax(MyDateUtil.float2Int1(invoiceMainInfo.getInvoiceTotalTax()));//合计税额
                pInvoiceRedRet.setInvoiceTotalPriceTax(MyDateUtil.float2Int1(invoiceMainInfo.getInvoiceTotalPriceTax()));//价税合计
            }
            pInvoiceRedService.update(pInvoiceRedRet);//保存数据
            return response;
        }
        catch (BopException e)
        {
            e.printStackTrace();
            return null;
        }
    }
}
